from flask import jsonify, request
from business_logic import example_logic, ssh_connect, ssh_execute_command, compare_versions, \
    process_data, analyze_strategy, parse_version, extract_device_info, compare_versions, analyze_strategies

# 注册路由
def register_routes(app):
    @app.route('/api/example', methods=['GET'])
    def example_api():
        # 调用业务逻辑层
        return jsonify(example_logic())
    
    # 新增：SSH执行命令路由
    @app.route('/api/ssh', methods=['POST'])
    def ssh_api():
        data = request.json
        hostname = data.get('hostname')
        port = data.get('port', 22)
        username = data.get('username')
        password = data.get('password')
        command = data.get('command')
        
        # 检查是否缺少必要的参数
        if not all([hostname, username, password, command]):
            return jsonify({"error": "Missing required parameters"}), 400
        
        try:
            # 连接到服务器
            client = ssh_connect(hostname, port, username, password)
            # 执行SSH命令
            result = ssh_execute_command(client, command)
            return jsonify(result)
        except Exception as e:
            # 捕获并返回异常信息
            return jsonify({"error": str(e)}), 500
    
    # 新增：版本比对路由
    @app.route('/api/compare_versions', methods=['POST'])
    def compare_versions_api():
        data = request.json
        try:
            # 比较版本号
            result = process_data(data)
            return jsonify({"result": result})
        except Exception as e:
            # 捕获并返回异常信息
            return jsonify({"error": str(e)}), 500
    
    # 新增：策略展示路由
    @app.route('/api/strategies', methods=['GET'])
    def strategies_api():
        # 假设从数据库或其他存储中获取策略列表
        strategies = [
            {
                "id": 1,
                "name": "Example Strategy",
                "type": "ACL",
                "info": {
                    "rules": [
                        {
                            "action": "allow",
                            "src_zone": "untrust",
                            "service": "tcp/3389",
                            "log": False
                        }
                    ]
                }
            }
        ]
        return jsonify(strategies)
    
    # 新增：策略清理路由
    @app.route('/api/strategies/cleanup', methods=['POST'])
    def cleanup_strategies_api():
        data = request.json
        strategy_ids = data.get('strategy_ids', [])
        # 假设根据 strategy_ids 清理策略
        cleaned_strategies = [s for s in strategies if s['id'] not in strategy_ids]
        return jsonify(cleaned_strategies)
    
    # 新增：策略优化路由
    @app.route('/api/strategies/optimize', methods=['POST'])
    def optimize_strategies_api():
        data = request.json
        strategy_ids = data.get('strategy_ids', [])
        # 假设根据 strategy_ids 优化策略
        optimized_strategies = [s for s in strategies if s['id'] in strategy_ids]
        for strategy in optimized_strategies:
            strategy['info']['optimized'] = True
        return jsonify(optimized_strategies)
    
    # 新增：策略分析路由
    @app.route('/api/strategies/analyze', methods=['POST'])
    def analyze_strategies_api():
        data = request.json
        strategy_ids = data.get('strategy_ids', [])
        # 假设根据 strategy_ids 分析策略
        analyzed_strategies = [s for s in strategies if s['id'] in strategy_ids]
        for strategy in analyzed_strategies:
            strategy['info']['analysis'] = analyze_strategy(json.dumps(strategy['info']))
        return jsonify(analyzed_strategies)